﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recRegistrationCurrentform : BasePage
    {
        private int id = -1;
        
        protected override void OnLoad(EventArgs e)
        {
       
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                
                recEmisionTypeRepo recEmisionTypeDal = new recEmisionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegistrationEmisionTypeRepo recRegistrationEmisionTypeDal = new recRegistrationEmisionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegistrationRepo recRegistratioDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                //populate dropdownlist Type Registrasi
                List<recRegistrationEmisionType> registrationEmisionTypes = recRegistrationEmisionTypeDal.GetList();
                ddlRegistrationEmisionType.Items.Clear();
                registrationEmisionTypes.ForEach(delegate(recRegistrationEmisionType registrationEmisionType)
                {
                    ddlRegistrationEmisionType.Items.Add(new ListItem(registrationEmisionType.RegistrationEmisionTypeName, registrationEmisionType.RegistrationEmisionTypeID.ToString()));
                });
                ddlRegistrationEmisionType.Items.Insert(0, new ListItem("-- pilih jenis registrasi emisi --", string.Empty));

                

                if (id > 0) // Edit an existing item
                {
                    recRegistrationRepo recRegistrationDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recRegistration recRegistration = recRegistrationDal.GetByIdEditItem(id);
                    if (recRegistration != null)
                    {

                        //populate dropdownlist Jenis Emisi
                        List<recEmisionType> emisionTypes = recEmisionTypeDal.GetList();
                        ddlJenisEmisi.Items.Clear();
                        emisionTypes.ForEach(delegate(recEmisionType emisionType)
                        {
                            ddlJenisEmisi.Items.Add(new ListItem(emisionType.EmisionTypeName, emisionType.EmisionTypeID.ToString()));
                        });
                        ddlJenisEmisi.Items.Insert(0, new ListItem("-- pilih Jenis Penawaran Umum  --", string.Empty));

                        
                        hfID.Value = id.ToString();

                    }
                }
                if (base.DebtorID.HasValue)
                {
                    hfDebtorID.Value = base.DebtorID.ToString();
                    recDebtor debtor = recDebtorDal.GetById(base.DebtorID.Value);
                    if (debtor != null)
                    {
                        hfNPWP.Value = debtor.NPWP;
                    }
                }
                else
                {
                    if (base.RegistrationID != 0)
                    {
                        hfDebtorID.Value = base.RegistrationID.ToString();
                        recRegistration recRegistration = new recRegistration();
                        recRegistration = recRegistratioDal.GetById(base.RegistrationID);
                        if (recRegistration != null)
                        {
                            hfNPWP.Value = recRegistration.NPWP;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(base.SubSectorName))
                {
                    hfSubSectorName.Value = base.SubSectorName.ToLower();
                    recRegistrationRepo recRegistrationDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recRegistration recRegistration = recRegistrationDal.GetByUserID(base.UserID);
                    if (recRegistration != null)
                    {
                        hfSectorID.Value = recRegistration.recSector.SectorID.ToString();
                        hfSectorCode.Value = recRegistration.recSector.SectorCode;
                        hfSectorName.Value = recRegistration.recSector.SectorName;

                        hfSubSectorID.Value = recRegistration.recSubSector.SubSectorID.ToString();
                        hfSubSectorCode.Value = recRegistration.recSubSector.SubSectorCode;
                        hfSubSectorNames.Value = recRegistration.recSubSector.SubSectorName;

                        hfSubSubSectorID.Value = recRegistration.recSubSubSector.SubSubSectorID.ToString();
                        hfSubSubSectorCode.Value = recRegistration.recSubSubSector.SubSubSectorCode;
                        hfSubSubSectorName.Value = recRegistration.recSubSubSector.SubSubSectorName;

                    }
                }
            }
        }
    }
}